<?php

namespace app\admin\model\business;

use app\admin\model\basic\Assets;
use app\admin\model\report\Finance;
use think\Model;
use traits\model\SoftDelete;

class OrderAssets extends Model
{
    use SoftDelete;


    // 表名
    protected $name = 'order_assets';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    /**
     * Notes: 收款单
     * User: AerYYY
     * Date: 2/6/24 8:07 PM
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function for_payment($params)
    {
        $assets_id = $params['assets_id'];
        $assets_price = $params['assets_price'];
        $assets_remarks = $params['assets_remarks'];

        $price = 0;
        $child = [];
        foreach ($assets_id as $key => $item) {
            if (!empty($item) && !empty($assets_price[$key]) && $assets_price[$key] > 0) {
                $assetsModel = new Assets();
                $assets = $assetsModel->where('id', $item)->find();
                if (empty($assets)) {
                    $this->error = '异常的账户信息';
                    return false;
                }
                $assets->balance = bcadd($assets->balance, $assets_price[$key], 2);
                $assets->save();
                $assetsLogModel = new AssetsLog();
                $data = [
                    'staff_id' => $params['staff_id'],
                    'assets_id' => $item,
                    'status' => Finance::STATUS_SK,
                    'order_no' => $params['order_no'],
                    'date' => $params['date'],
                    'be' => 1,
                    'price' => $assets_price[$key],
                ];
                $assetsLogModel->createLog($data);
                $price = bcadd($price, $assets_price[$key], 2);
                $child[] = [
                    'order_no' => $params['order_no'],
                    'assets_id' => $item,
                    'price' => $assets_price[$key],
                    'remarks' => $assets_remarks[$key],
                ];
            }
        }

        if (empty($price)) {
            $this->error = '错误的金额';
            return false;
        }

        if (count($child) <= 0) {
            $this->error = '无效的账户数据';
            return false;
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'],
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'price' => $price,
            'remarks' => $params['remarks'],
            'status' => Finance::STATUS_SK,
        ]);

        $orderAssetsChildModel = new OrderAssetsChild();
        $orderAssetsChildModel->saveAll($child);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 1,
            'price' => $price,
            'status' => Finance::STATUS_SK
        ];
        $financeModel->save($params);

        return true;

    }

    public function bill_payment($params)
    {
        $assets_id = $params['assets_id'];
        $assets_price = $params['assets_price'];
        $assets_remarks = $params['assets_remarks'];

        $price = 0;
        $child = [];
        foreach ($assets_id as $key => $item) {
            if (!empty($item) && !empty($assets_price[$key]) && $assets_price[$key] > 0) {
                $assetsModel = new Assets();
                $assets = $assetsModel->where('id', $item)->find();
                if (empty($assets)) {
                    $this->error = '异常的账户信息';
                    return false;
                }
                $assets->balance = bcsub($assets->balance, $assets_price[$key], 2);
                $assets->save();
                $assetsLogModel = new AssetsLog();
                $data = [
                    'staff_id' => $params['staff_id'],
                    'assets_id' => $item,
                    'status' => Finance::STATUS_FK,
                    'order_no' => $params['order_no'],
                    'date' => $params['date'],
                    'be' => 2,
                    'price' => $assets_price[$key],
                ];
                $assetsLogModel->createLog($data);
                $price = bcadd($price, $assets_price[$key], 2);
                $child[] = [
                    'order_no' => $params['order_no'],
                    'assets_id' => $item,
                    'price' => $assets_price[$key],
                    'remarks' => $assets_remarks[$key],
                ];
            }
        }

        if (empty($price)) {
            $this->error = '错误的金额';
            return false;
        }

        if (count($child) <= 0) {
            $this->error = '无效的账户数据';
            return false;
        }

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'],
            'unit_id' => $params['unit_id'],
            'staff_id' => $params['staff_id'],
            'price' => $price,
            'remarks' => $params['remarks'],
            'status' => Finance::STATUS_FK,
        ]);

        $orderAssetsChildModel = new OrderAssetsChild();
        $orderAssetsChildModel->saveAll($child);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'unit_id' => $params['unit_id'],
            'be' => 2,
            'price' => $price,
            'status' => Finance::STATUS_FK
        ];
        $financeModel->save($params);

        return true;
    }


    public function transfer_payment($params)
    {
        $assets_id = $params['assets_id'];
        $assets_price = $params['assets_price'];
        $assets_remarks = $params['assets_remarks'];

        $price = 0;
        $child = [];
        foreach ($assets_id as $key => $item) {
            if (!empty($item) && !empty($assets_price[$key]) && $assets_price[$key] > 0) {
                $assetsModel = new Assets();
                $assets = $assetsModel->where('id', $item)->find();
                if (empty($assets)) {
                    $this->error = '异常的账户信息';
                    return false;
                }
                $assets->balance = bcadd($assets->balance, $assets_price[$key], 2);
                $assets->save();
                $assetsLogModel = new AssetsLog();
                $data = [
                    'staff_id' => $params['staff_id'],
                    'assets_id' => $item,
                    'status' => Finance::STATUS_TZC,
                    'order_no' => $params['order_no'],
                    'date' => $params['date'],
                    'be' => 1,
                    'price' => $assets_price[$key],
                ];
                $assetsLogModel->createLog($data);
                $price = bcadd($price, $assets_price[$key], 2);
                $child[] = [
                    'order_no' => $params['order_no'],
                    'assets_id' => $item,
                    'price' => $assets_price[$key],
                    'remarks' => $assets_remarks[$key],
                ];
            }
        }

        if (empty($price)) {
            $this->error = '错误的金额';
            return false;
        }

        if (count($child) <= 0) {
            $this->error = '无效的账户数据';
            return false;
        }

        $assetsModel = new Assets();
        $assets = $assetsModel->where('id', $params['order_assets_id'])->find();
        if (empty($assets)) {
            $this->error = '异常的账户信息';
            return false;
        }
        $assets->balance = bcsub($assets->balance, $price, 2);
        $assets->save();
        $assetsLogModel = new AssetsLog();
        $data = [
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['order_assets_id'],
            'status' => Finance::STATUS_TZC,
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'be' => 2,
            'price' => $price,
        ];
        $assetsLogModel->createLog($data);

        $this->save([
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'admin_id' => $params['admin_id'],
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['order_assets_id'],
            'price' => $price,
            'remarks' => $params['remarks'],
            'status' => Finance::STATUS_TZC,
        ]);

        $orderAssetsChildModel = new OrderAssetsChild();
        $orderAssetsChildModel->saveAll($child);

        $financeModel = new Finance();
        $params = [
            'order_no' => $params['order_no'],
            'date' => $params['date'],
            'staff_id' => $params['staff_id'],
            'assets_id' => $params['order_assets_id'],
            'be' => 2,
            'price' => $price,
            'status' => Finance::STATUS_TZC
        ];
        $financeModel->save($params);

        return true;
    }
}